
//
//  HomePageTableViewController.m
//  BakeCenter
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 Baked. All rights reserved.
//

#import "HomePageTableViewController.h"
#import "HomePageHeadView.h"
#import "HomePageTableViewCell.h"
#import "RecommendModel.h"
@interface HomePageTableViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)HomePageHeadView *headScrlloView;
@property (nonatomic, strong)NSMutableArray *imageArray;
@property (nonatomic, strong)NSMutableArray *titleArray;
@property (nonatomic, strong)NSMutableArray *recommendArray;

@end

@implementation HomePageTableViewController
//轮播图数据解析
-(void)setUpScrollViewData{
    [LORequestManger GET:kHeadScroolViewUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *item in dic[@"data"][@"category"][0][@"item"]) {
            NSString *imageUrl = [item valueForKey:@"image"];
            NSString *title = [item valueForKey:@"title"];
            if (![[item valueForKey:@"categoryItemId"] isEqualToString:@"10463"]) {
                [self.imageArray addObject:imageUrl];
                [self.titleArray addObject:title];
            }
            
        }
         [self setUpCoverFlowView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(NSMutableArray *)titleArray{
    if (!_titleArray) {
        _titleArray = [[NSMutableArray alloc] init];
    }
    return _titleArray;
}

-(NSMutableArray *)imageArray{
    if (!_imageArray) {
        _imageArray = [[NSMutableArray alloc] init];
    }
    return _imageArray;
}

-(void)reloadRecommendData{
    [LORequestManger GET:kRecommendUrl success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *data in dic[@"data"][@"collection"][@"component"][@"data"]) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:data];
            [self.recommendArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(NSMutableArray *)recommendArray{
    if (!_recommendArray) {
        _recommendArray = [[NSMutableArray alloc] init];
    }
    return _recommendArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *TView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    CUSFlashLabel *titleLabel = [[CUSFlashLabel alloc] initWithFrame:TView.bounds];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"BakeHome";
    titleLabel.spotlightColor = [UIColor redColor];
    titleLabel.font = [UIFont fontWithName:@"Zapfino" size:15];
    [titleLabel startAnimating];
    [TView addSubview:titleLabel];
    
    self.navigationItem.titleView = TView;
    
    self.tableView.backgroundView = [[BlurImageView alloc] initWithFrame:kScreenBounds];
    self.tableView.backgroundColor = [UIColor clearColor];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self setUpScrollViewData];
    [self reloadRecommendData];
   

}


-(void)setUpCoverFlowView{
    
    //设置 headScrlloView 设置定时器时间和方法
    self.headScrlloView = [[HomePageHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 170)imageArray:_imageArray titleArray:self.titleArray delegate:self action:@selector(touchImage) timer:3.5 selector:@ selector(actionTimer)];
    self.headScrlloView.delegate = self;
    self.tableView.tableHeaderView = self.headScrlloView;
}

-(void)touchImage{
    //当我们的index最后一张的时候 我们将
    int index = self.headScrlloView.contentOffset.x / kScreenWidth - 1;
    if (index == self.imageArray.count) {
        index = 0;
    }
    NSLog(@"%d",index);
    
    
}

//计时器方法
-(void)actionTimer{
    
    [self scrollViewDidCircularly];
    CGPoint newPoint = self.headScrlloView.contentOffset;
    newPoint.x += kScreenWidth;
    int X = newPoint.x / kScreenWidth;
    newPoint.x = X * kScreenWidth;
    [self.headScrlloView setContentOffset:newPoint animated:YES];
    
    
}

//实现轮播图
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    [self scrollViewDidCircularly];
    
}


//封装循环滚动的判断方法
-(void)scrollViewDidCircularly{
    if (self.headScrlloView.contentOffset.x == 0) {
        [self.headScrlloView setContentOffset:CGPointMake(kScreenWidth * self.imageArray.count, 0)animated:NO];
    }
    if (self.headScrlloView.contentOffset.x >= kScreenWidth * (self.imageArray.count + 1)) {
        [self.headScrlloView setContentOffset:CGPointMake(kScreenWidth, 0)animated:NO];
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.recommendArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       static NSString *identifier = @"homePageCell";
    
    HomePageTableViewCell *homePageCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (homePageCell == nil) {
        homePageCell = [[HomePageTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    // Configure the cell...
    
        RecommendModel *model = self.recommendArray[indexPath.row];
        homePageCell.title.text = model.coverTitle;
        [homePageCell.pic sd_setImageWithURL:[NSURL URLWithString:model.coverImage] placeholderImage:[UIImage imageNamed:@"10876719,2880,1800"]];
        homePageCell.discription.text = model.coverSummary;
    
    
    return homePageCell;
}

//tableView点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    MakeDetialTableViewController *makeDetialTVC = [[MakeDetialTableViewController alloc] initWithStyle:(UITableViewStylePlain)];
    RecommendModel *model = self.recommendArray[indexPath.row];
    makeDetialTVC.makeDetialUrl = [NSString stringWithFormat:@"http://www.hongbeibang.com/recipe/get?t=1442738615372&contentId=%@&_ios=true&_=1442735205649",model.contentId];
    makeDetialTVC.lastView = @"recommend";kRecommendMakeDetialUrl
    [self.navigationController setAnimationWithSubtype:1 andAnimationType:8];
    
    [self.navigationController pushViewController:makeDetialTVC animated:YES];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}







/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
